import Vue from 'vue'
import Vuex from 'vuex'
import { auInfo } from '../api/my'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 共享数据
    userInfo: '',
    isLogin: false
  },
  mutations: {
    // 用于修改state内的数据，唯一的修改方式
    setUserInfo (state, value) {
      state.userInfo = value
    },
    setLoginStatus (state, value) {
      state.isLogin = value
    },
    setUserInfoOther (state, obj) {
      state.userInfo[obj.key] = obj.value
    }
    // 调用方法 this.$store.commit(方法名)
  },
  actions: {
    // 数据刷新
    async refreshUserInfo (store) {
      const res = await auInfo()
      store.commit('setUserInfo', res.data.data)
    }
    // 调用方法 this.$store.dispatch(方法名)
  },
  getters: {
    getCollect (state) {
      return (name, id) => {
        return state.isLogin && state.userInfo[name].includes(+id)
      }
    },
    getCollectTotal (state) {
      return state.userInfo === '' ? 0 : state.userInfo.collectQuestions.length
    }
    //调用:this.$store.getters.方法名
  },
  modules: {}
})
